<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends BaseController {
    public function index(){
        $dish = M('dish')->select();
        $this->display();
    }

    public function showMenu(){
        $dish = M('dish')->select();
        $pagesize = 5;#每页数量
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $count = count($dish);
        $offset = $pagesize * ($p - 1);//计算记录偏移量

        $dish= M('dish')
            ->limit($offset . ',' . $pagesize)
            ->select();

        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('page', $page);

        $this->assign('menu', $dish);
        $this->display();
    }
    public function add(){
        $dish['dish_icon'] = 'Public/admin/img/photo_icon.png';
        $this->assign("dish",$dish);
        $this->display();
    }
    public function upload(){
        $arr['result']=1;
        $arr['show']='正确';
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =      'Uploads/Images/'; // 设置附件上传根目录
        $upload->savePath  =      ''; // 设置附件上传（子）目录
        // 上传文件
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $arr['result']=0;
            $arr['show']=$upload->getError();
        }else{// 上传成功 获取上传文件信息
            foreach($info as $file){
                if($arr['result']!=0){
                    $arr['result']=1;
                    $arr['show']=$file['savepath'].$file['savename'];
                }
            }
        }
        $this->ajaxReturn($arr,'json');

    }
    public function updateInfo(){
        $dish_id =I('dish_id');
        $this->assign("dish",M('dish')->find($dish_id));
        $this->display('add');
    }

    public function delete(){
        $getId = $_REQUEST['id'];
        if (M('dish')->delete($getId)){
            $this->success('删除成功', U('Index/showMenu'));
        }else{
            $this->error('删除失败');
        }

    }


    public function update(){
        $dish_name = isset($_POST['dish_name']) ? trim($_POST['dish_name']) : '';
        $dish_description= isset($_POST['dish_description']) ? trim($_POST['dish_description']) : '';
        $dish_pubdate = isset($_POST['dish_pubdate']) ? trim($_POST['dish_pubdate']) : '';
        $dish_id =I('dish_id');
        $dish_num = isset($_POST['dish_num']) ? intval($_POST['dish_num']) : 0;
        $dish_price = isset($_POST['dish_price']) ? intval($_POST['dish_price']) : 0;
        $data['dish_name']=$dish_name;

        $data['dish_description']=$dish_description;
        $data['dish_pubdate']=$dish_pubdate;
        $data['dish_id']=$dish_id;
        $data['dish_num']=$dish_num;
        $data['dish_price']=$dish_price;
        $dish = M('dish');
        $imghead = I('imgUrl');

        if(empty($dish_id)){
            $data['dish_icon']=$imghead;

          if (  $dish->add($data)){
              $this->success('添加成功', U('Index/add'));
          }else{
              $this->error('添加失败');
          }
        }else{
            $data['dish_icon']=$imghead;
           if ( $dish->where('dish_id='.$dish_id)->save($data)){
               $this->success('保存成功,正跳转至系统首页...', U('Index/showMenu'));
           }else{
               $this->error('保存失败');
           }
        }
    }

}